package com.anhry.www.safety.util;
/**
 * @date:2011-11-12
 * @remark:
 * @author:dongbin
 */
import java.awt.Image;   
import java.awt.image.BufferedImage;   
import java.io.File;   
import java.io.FileNotFoundException;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.io.InputStream;   
  
import javax.imageio.ImageIO;   
  
import com.sun.image.codec.jpeg.JPEGCodec;   
import com.sun.image.codec.jpeg.JPEGEncodeParam;   
import com.sun.image.codec.jpeg.JPEGImageEncoder;   
  
public class ImageZipUtil {   
  
    /**  
     * 压缩图片文件<br>  
     * 先保存原文件，再压缩、上传  
     *   
     * @param oldFile  
     *            要进行压缩的文件全路径  
     * @param width  
     *            宽度  
     * @param height  
     *            高度  
     * @param quality  
     *            质量  
     * @param smallIcon  
     *            小图片的后缀  
     * @return 返回压缩后的文件的全路径  
     */  
    public static String zipImageFile(String oldFile, int width, int height,   
            float quality, String smallIcon) {   
        if (oldFile == null) {   
            return null;   
        }   
        String newImage = null;   
        try {   
            /** 对服务器上的临时文件进行处理 */  
            Image srcFile = ImageIO.read(new File(oldFile));
            int w = srcFile.getWidth(null);//原来宽度
            int h = srcFile.getHeight(null);//原来高度
            width = w;
            height = h;
            int tem = (w*h)/(800*600);
            if(tem>1){
            	if(w>h){//如果原来图片宽大于高
            		width = w/(w/800);
            		height = h/(w/800);
            	}else{
            		width = w/(h/800);
            		height = h/(h/800);
            	}
            }
            System.out.println(width);
            System.out.println(height);
            //width = w/4;
            //height = h/4;
            
            /** 宽,高设定 */  
            BufferedImage tag = new BufferedImage(width, height,   
                    BufferedImage.TYPE_INT_RGB);   
            tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);   
            String filePrex = oldFile.substring(0, oldFile.indexOf('.'));   
            /** 压缩后的文件名 */  
            newImage = filePrex   
                    + oldFile.substring(filePrex.length());   
            
  
            /** 压缩之后临时存放位置 */  
            FileOutputStream out = new FileOutputStream(newImage);   
  
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   
            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);   
            /** 压缩质量 */  
            jep.setQuality(quality, true);   
            encoder.encode(tag, jep);   
            out.close();   
  
        } catch (FileNotFoundException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        return newImage;   
    }   
  
    /**  
     * 保存文件到服务器临时路径  
     *   
     * @param fileName  
     * @param is  
     * @return 文件全路径  
     */  
    public static String writeFile(String fileName, InputStream is) {   
        if (fileName == null || fileName.trim().length() == 0) {   
            return null;   
        }   
        try {   
            /** 首先保存到临时文件 */  
            FileOutputStream fos = new FileOutputStream(fileName);   
            byte[] readBytes = new byte[512];// 缓冲大小   
            int readed = 0;   
            while ((readed = is.read(readBytes)) > 0) {   
                fos.write(readBytes, 0, readed);   
            }   
            fos.close();   
            is.close();   
        } catch (FileNotFoundException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        return fileName;   
    }   
    public static void main(String args[]){   
        ImageZipUtil.zipImageFile("D:/Tomcat 5.5/webapps/shySafety/uploadfile/msg_folder/1/1321602762833.JPG", 600, 800, 1f, "x2");   
           
    }   
}
